package com.wfuhui.modules.order.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.wfuhui.modules.order.dao.OrderDao;
import com.wfuhui.modules.order.entity.OrderEntity;
import com.wfuhui.modules.order.entity.OrderRefundEntity;
import com.wfuhui.modules.order.service.OrderRefundService;
import com.wfuhui.modules.order.service.OrderService;
import com.wfuhui.common.utils.NumberUtil;
import com.wfuhui.common.utils.Query;
import com.wfuhui.common.utils.R;

import javax.annotation.Resource;


/**
 * 订单退款
 * 
 * @author lizhengle
 * @email codemax@qq.com
 * @date 07-27 12:19:44
 */
@RestController
@RequestMapping("/mall/orderrefund")
public class OrderRefundController {
	@Autowired
	private OrderRefundService orderRefundService;
	@Resource(name = "wxPayService")
	private WxPayService wxService;
	@Autowired
	private OrderService orderService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<OrderRefundEntity> orderReturnList = orderRefundService.queryList(query);
		int total = orderRefundService.queryTotal(query);
		
		return R.ok().put("rows", orderReturnList).put("total", total);
		
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Integer id){
		OrderRefundEntity orderReturn = orderRefundService.queryObject(id);
		
		return R.ok().put("orderReturn", orderReturn);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody OrderRefundEntity orderReturn){
		orderReturn.setRefundNumber(NumberUtil.getOrderNumber());
		orderReturn.setCreateTime(new Date());
		orderRefundService.save(orderReturn);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("mall:orderrefund:update")
	public R update(@RequestBody OrderRefundEntity orderReturn){
		orderRefundService.update(orderReturn);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("mall:orderrefund:delete")
	public R delete(@RequestBody Integer[] ids){
		orderRefundService.deleteBatch(ids);
		
		return R.ok();
	}


	@GetMapping("refundOk")
	@RequiresPermissions("mall:orderrefund:update")
	public R refundOk(Integer id) throws WxPayException {
		OrderRefundEntity orderRefund = orderRefundService.queryObject(id);
		if(orderRefund.getStatus() != 1) {
			return R.error("已审核，请勿再次审核");
		}
		orderRefundService.refund(orderRefund);
		return R.ok();
	}

	@GetMapping("refundFail")
	@RequiresPermissions("mall:orderrefund:update")
	public R refundFail(Integer id, String refuseRemark) throws WxPayException {
		OrderRefundEntity orderRefund = orderRefundService.queryObject(id);
		if(orderRefund.getStatus() != 1) {
			return R.error("已审核，请勿再次审核");
		}
		OrderRefundEntity o = new OrderRefundEntity();
		o.setId(id);
		o.setStatus(3);
		o.setRefuseRemark(refuseRemark);
		orderRefundService.update(o);
		return R.ok();
	}
	
}
